<?php
class NewslettersController extends NewslettersAppController {

	var $name = 'Newsletters';
	var $helpers = array('Html', 'Form');
	var $components = array('Email', 'Session');
	var $paginate = array('limit' => 10, 'page' => 1, 'order'=>array('Newsletter.id'=>'desc'));
	var $uses = array('Newsletters.Newsletter', 'Newsletters.Subscriber');
	
	function admin_index() {
		$this->pageTitle = __('Newsletters');
		$this->set('newsletters', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Newsletter', true));
			$this->redirect(array('action'=>'index'));
		}
		$newsletter = $this->Newsletter->read(null, $id);
		$this->set('newsletter', $newsletter);
	}

	function admin_view_body($id = null, $type) {
		Configure::write('debug',0);
		if (!$id) {
			$this->Session->setFlash(__('Invalid Newsletter', true));
			$this->redirect(array('action'=>'index'));
		}
		$newsletter = $this->Newsletter->read(null, $id);
		if(isset($newsletter['Newsletter']['newsletter_type']) && !is_null($newsletter['Newsletter']['newsletter_type']) && $newsletter['Newsletter']['newsletter_type'])
		{
			$newsletter['Newsletter']['body_html'] = $this->Newsletter->getContentForNewsletterType($newsletter['Newsletter']['newsletter_type']);
			$newsletter['Newsletter']['body_text'] = $newsletter['Newsletter']['body_html'];
		}
		$type = 'body_'.$type;
		$newsletter['Newsletter']['body'] =$newsletter['Newsletter'][$type];
		$this->set('newsletter', $newsletter);
		$this->set('type', $type);
		$this->layout = '';
	}

	function admin_add() 
	{
		if (!empty($this->data))
		{
			$this->Newsletter->create();
			if ($this->Newsletter->save($this->data))
			{
				$this->successMsg(__('Newsletter saved'));
				$this->redirect(array('action'=>'admin_index'));
			} 
		}
		$newsletterTypes = $this->Newsletter->getNewsletterTypeOptions();
		$this->set("newsletterTypes", $newsletterTypes);
	}

	function admin_edit($id = null) 
	{
		$this->Newsletter->recursive = -1;
		$newsletter = $this->Newsletter->findById($id);
		if(!$newsletter)
			$this->redirect($this->referer());
		if($this->data)
		{
			$this->data['Newsletter']['id'] = $id;
			$this->admin_add();
		}else
		{
			$this->data = $newsletter;
		}
		$newsletterTypes = $this->Newsletter->getNewsletterTypeOptions();
		$this->set("newsletterTypes", $newsletterTypes);
		$this->render('admin_add');
	}
	function admin_delete($id = null) {
		if (!$id) {
			$this->errorMsg(__('Invalid Newsletter', true), array('controller'=>'newsletters','action'=>'index'));
		}
		if ($this->Newsletter->del($id)) {
			$this->successMsg(__('The Newsletter has been deleted.', true));
		}
		$this->redirect(array('controller'=>'newsletters','action'=>'admin_index'));
	}

}
 ?>